package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.OrderPInfoBean;
import com.project.bean.ProductBean;
import com.project.bean.PurchaseCarBean;
import com.project.bean.PurchaseOrderBean;
import com.project.mapper.IOderPInfoMapper;
import com.project.mapper.IProductMapper;
import com.project.mapper.IPurchaseCarMapper;
import com.project.mapper.IPurchaseOrderMapper;
import com.project.service.IPurchaseCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 购物车实现类
 */
@Service
@Transactional
public class PurchaseCarServiceImpl implements IPurchaseCarService {
    @Autowired
    private IPurchaseCarMapper purchaseCarMapper;
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private IPurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private IOderPInfoMapper oderPInfoMapper;
    @Override
    public List<PurchaseCarBean> findAll(String userName) {
        return purchaseCarMapper.findAll(userName);
    }

    @Override
    public void updatePriceNumById(Integer id, Integer newPrice, Integer newNum) {
           PurchaseCarBean purchaseCar=purchaseCarMapper.selectById(id);
        purchaseCar.setNum(newNum);
        purchaseCar.setTranPrice(newPrice);

           purchaseCarMapper.updateById(purchaseCar);
    }

    @Override
    public void removeShop(Integer id) {
        purchaseCarMapper.deleteById(id);
    }

    @Override
    public void addPurchaseCar(ProductBean product,Integer num,String userName) {
        //查询库存商品
         QueryWrapper<ProductBean> qw=new QueryWrapper<>();
         qw.eq("pk_productId",product.getProductId());
         ProductBean productObj=productMapper.selectOne(qw);
         if (productObj !=null){//库存已有该商品
             QueryWrapper<PurchaseCarBean> qwp=new QueryWrapper<>();
             qwp.eq("fk_productId",productObj.getProductId());
             qwp.eq("p_userName",userName);
             PurchaseCarBean purchaseCar=purchaseCarMapper.selectOne(qwp);
             if (purchaseCar !=null){//购物车已有该商品，数量累加
                 purchaseCar.setNum(purchaseCar.getNum()+num);
                 purchaseCarMapper.updateById(purchaseCar);
             }else {//购物车不存在该商品，添加商品
                purchaseCarMapper.insert(new PurchaseCarBean(productObj.getProductId(),num,userName));
             }
         }else {//库存无此商品，添加商品
             productMapper.insert(product);
             purchaseCarMapper.insert(new PurchaseCarBean(product.getProductId(),num,userName));
         }
    }

    @Override
    public void addProduct(PurchaseOrderBean purchaseOrder,String userName) {
        //添加采购订单
        purchaseOrderMapper.insert(purchaseOrder);
        //按采购员查询购物车
        QueryWrapper<PurchaseCarBean> qw=new QueryWrapper<>();
       qw.eq("p_userName",userName);
        List<PurchaseCarBean> purchaseCarObj=purchaseCarMapper.selectList(qw);
        //遍历采购车，循环添加采购明细信息，循环修改库存商品的库存数量（累加）、以及最新采购价
        for (PurchaseCarBean pur: purchaseCarObj) {
            //遍历采购车，循环添加采购明细信息
            OrderPInfoBean orderPInfoObj=new OrderPInfoBean();
            orderPInfoObj.setPoductId(pur.getProductId());
            orderPInfoObj.setNum(pur.getNum());
            orderPInfoObj.setPrice(pur.getTranPrice());
            orderPInfoObj.setPurchaseOrderId(purchaseOrder.getPoId());
            oderPInfoMapper.insert(orderPInfoObj);
            //循环修改库存商品的库存数量（累加）、以及最新采购价
            ProductBean product=productMapper.selectById(pur.getProductId());
            if(product.getStock()==null){
                product.setStock(0);
            }
            product.setStock(product.getStock()+pur.getNum());
            product.setGetPrice(pur.getTranPrice());
            productMapper.updateById(product);

        }
        //根据采购员id清空购物车
        QueryWrapper<PurchaseCarBean> qwc=new QueryWrapper<>();
        qwc.eq("p_userName",userName);
        purchaseCarMapper.delete(qw);
    }
}
